package com.itogis.interceptors.autoWebEvent;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.Element;


public class WebStatistics2 implements java.io.Serializable {
	private static final long serialVersionUID = 2269176464746661722L;

	public static WebStatistics2 parseFromXml(Document doc)
			throws Exception {
		WebStatistics2 result = new WebStatistics2();
		Element rootEle = doc.getRootElement();

		result.setUserId(getElementText(rootEle, "userToken"));
		result.setTitle(getElementText(rootEle, "title"));
		result.setKeywords(getElementText(rootEle, "keywords"));
		result.setPath(getElementText(rootEle, "path"));
		result.setContent(getElementText(rootEle, "content"));
		result.setHref(getElementText(rootEle, "href"));
		result.setOnclick(getElementText(rootEle, "onclick"));
		result.setCreateDate(new Date());
		return result;
	}

	private static String getElementText(Element parent, String elementName) {
		String value = "";
		Element ele = parent.element(elementName);
		if (null != ele) {
			value = ele.getTextTrim();
		} else {
			System.out.println("WARN: [" + elementName + "] not exists!");
		}
		return value;
	}

	private static final String URL_ARGS_TITLE = "a";
	private static final String URL_ARGS_KEYWORDS = "b";
	private static final String URL_ARGS_PATH = "c";
	private static final String URL_ARGS_CONTENT = "d";
	private static final String URL_ARGS_HREF = "e";
	private static final String URL_ARGS_ONCLICK = "f";

	public static WebStatistics2 parseFromUrl(HttpServletRequest request)
			throws UnsupportedEncodingException {
		String urlCharset = "UTF-8";
		String title = request.getParameter(URL_ARGS_TITLE);
		String keywords = request.getParameter(URL_ARGS_KEYWORDS);
		String path = request.getParameter(URL_ARGS_PATH);
		String content = request.getParameter(URL_ARGS_CONTENT);
		String href = request.getParameter(URL_ARGS_HREF);
		String onclick = request.getParameter(URL_ARGS_ONCLICK);

		WebStatistics2 result = new WebStatistics2();
		result.setTitle(URLDecoder.decode(title, urlCharset));
		result.setKeywords(URLDecoder.decode(keywords, urlCharset));
		result.setPath(URLDecoder.decode(path, urlCharset));
		result.setContent(URLDecoder.decode(content, urlCharset));
		result.setHref(URLDecoder.decode(href, urlCharset));
		result.setOnclick(URLDecoder.decode(onclick, urlCharset));
		result.setCreateDate(new Date());
		return result;
	}

	public static WebStatistics2 parseFromForm(HttpServletRequest request)
			throws UnsupportedEncodingException {
		WebStatistics2 result = new WebStatistics2();
		result.setTitle(String.valueOf(request.getParameter("title")));
		result.setKeywords(String.valueOf(request.getParameter("keywords")));
		result.setPath(String.valueOf(request.getParameter("path")));
		result.setContent(String.valueOf(request.getParameter("content")));
		result.setHref(String.valueOf(request.getParameter("href")));
		result.setOnclick(String.valueOf(request.getParameter("onclick")));
		result.setCreateDate(new Date());
		return result;
	}

	private Long id;
	private String userId;
	private String title;
	private String keywords;
	private String path;
	private String content;
	private String href;
	private String onclick;
	private Date createDate;

	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	
	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}


	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	
	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}


	public String getOnclick() {
		return onclick;
	}

	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((content == null) ? 0 : content.hashCode());
		result = prime * result
				+ ((createDate == null) ? 0 : createDate.hashCode());
		result = prime * result + ((href == null) ? 0 : href.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((keywords == null) ? 0 : keywords.hashCode());
		result = prime * result + ((onclick == null) ? 0 : onclick.hashCode());
		result = prime * result + ((path == null) ? 0 : path.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		result = prime * result + ((userId == null) ? 0 : userId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final WebStatistics2 other = (WebStatistics2) obj;
		if (content == null) {
			if (other.content != null)
				return false;
		} else if (!content.equals(other.content))
			return false;
		if (createDate == null) {
			if (other.createDate != null)
				return false;
		} else if (!createDate.equals(other.createDate))
			return false;
		if (href == null) {
			if (other.href != null)
				return false;
		} else if (!href.equals(other.href))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (keywords == null) {
			if (other.keywords != null)
				return false;
		} else if (!keywords.equals(other.keywords))
			return false;
		if (onclick == null) {
			if (other.onclick != null)
				return false;
		} else if (!onclick.equals(other.onclick))
			return false;
		if (path == null) {
			if (other.path != null)
				return false;
		} else if (!path.equals(other.path))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		if (userId == null) {
			if (other.userId != null)
				return false;
		} else if (!userId.equals(other.userId))
			return false;
		return true;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("SpStatistics [id=");
		builder.append(id);
		builder.append(", userId=");
		builder.append(userId);
		builder.append(", title=");
		builder.append(title);
		builder.append(", keywords=");
		builder.append(keywords);
		builder.append(", path=");
		builder.append(path);
		builder.append(", content=");
		builder.append(content);
		builder.append(", href=");
		builder.append(href);
		builder.append(", onclick=");
		builder.append(onclick);
		builder.append(", createDate=");
		builder.append(createDate);
		builder.append("]");
		return builder.toString();
	}
}
